package dk.mirasola.systemtraining.user.client.ui.createbiddingsetwizard;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import dk.mirasola.systemtraining.bridgewidgets.shared.model.Distribution;
import dk.mirasola.systemtraining.bridgewidgets.shared.model.Seat;
import dk.mirasola.systemtraining.user.client.i18n.CreateBiddingSetWizardTexts;
import dk.mirasola.systemtraining.user.client.ui.wizard.WizardStep;
import dk.mirasola.systemtraining.user.shared.transfer.CreateBiddingSetTO;


class GenerateBoardsStep implements WizardStep {
    interface GenerateBoardsStepUiBinder extends UiBinder<Widget,GenerateBoardsStep>{}
    private static GenerateBoardsStepUiBinder uiBinder = GWT.create(GenerateBoardsStepUiBinder.class);

    private CreateBiddingSetWizardTexts texts;
    private CreateBiddingSetTO createBiddingSetTO;
    private Button backButton;
    private Button finishButton;
    private Widget stepPanel;
    private boolean stop = false;

    private ClickHandler stopHandler = new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            stopGenerateButton.setEnabled(false);
            stop = true;
        }
    };

    @UiField
    Label runningCount;
    @UiField
    Button stopGenerateButton;
    @UiField
    HTML header;

    GenerateBoardsStep(CreateBiddingSetTO createBiddingSetTO, final CreateBiddingSetWizardTexts texts,
                       Button backButton, Button finishButton, Button cancelButton) {
        this.createBiddingSetTO = createBiddingSetTO;
        this.texts = texts;
        this.backButton = backButton;
        this.finishButton = finishButton;
        cancelButton.addClickHandler(stopHandler);

        stepPanel = uiBinder.createAndBindUi(this);

        initStopButton();
        header.setHTML(texts.generateBoardsHeader());
    }

    private void initStopButton() {
        stopGenerateButton.setHTML(texts.stopGeneratorButton());
        stopGenerateButton.addClickHandler(stopHandler);
        stopGenerateButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                backButton.setEnabled(true);
                if (createBiddingSetTO.getDistributions().size() > 0) {
                    finishButton.setEnabled(true);
                }
            }
        });
    }

    public SafeHtml getProgressText() {
        return texts.generateBoardsStepProgressText();
    }

    public Widget getWidget() {
        return stepPanel;
    }

    public void activate() {
        backButton.setEnabled(false);
        finishButton.setEnabled(false);
        stopGenerateButton.setEnabled(true);
        stopGenerateButton.setFocus(true);
        stop = false;

        Timer timer = new Timer() {
            @Override
            public void run() {
                if (stop) {
                    finishButton.setEnabled(createBiddingSetTO.getDistributions().size() > 0);
                    backButton.setEnabled(true);
                    return;
                }

                for (int i = 0; i < 1000; i++) {
                    Distribution shuffled = Distribution.shuffledDistribution();
                    if (createBiddingSetTO.getDistributionFilter().matches(shuffled)) {
                        shuffled.setHand(Seat.EAST,null);
                        shuffled.setHand(Seat.WEST,null);
                        createBiddingSetTO.getDistributions().add(shuffled);
                    }
                    if (createBiddingSetTO.getDistributions().size() == createBiddingSetTO.getBoards()) {
                        break;
                    }
                }

                runningCount.setText(String.valueOf(createBiddingSetTO.getDistributions().size()));

                if (createBiddingSetTO.getDistributions().size() == createBiddingSetTO.getBoards()) {
                    stopGenerateButton.setEnabled(false);
                    backButton.setEnabled(true);
                    finishButton.setEnabled(true);
                } else {
                    schedule(10);
                }
            }
        };

        timer.schedule(10);
    }
}
